local blockSprite = lg.newImage("sprites/block.png")
blockSprite:setWrap("repeat")

local snowflake = lg.newImage("sprites/snowflake.png") -- Preset: circle

local ps = love.graphics.newParticleSystem(snowflake, 56)
ps:setColors(1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0.5, 1, 1, 1, 0)
ps:setDirection(1.5)
ps:setEmissionArea("uniform", 295.3, 0, 0, false)
ps:setEmissionRate(20)
ps:setEmitterLifetime(1.24)
ps:setInsertMode("bottom")
ps:setLinearAcceleration(0.05, 0, 0, 0)
ps:setLinearDamping(0, 0)
ps:setOffset(50, 50)
ps:setParticleLifetime(2.9, 1.7)
ps:setRadialAcceleration(0, 0)
ps:setRelativeRotation(false)
ps:setRotation(0, 1)
ps:setSizes(0.1, 0.3)
ps:setSizeVariation(1)
ps:setSpeed(100, 150)
ps:setSpin(0, 0.5)
ps:setSpinVariation(0)
ps:setSpread(1.28)
ps:setTangentialAcceleration(0, 0)
-- At start time:
-- At draw time:
-- love.graphics.setBlendMode("add")
-- love.graphics.draw(ps, 92+0, -102+0)

local Object = {
	x = 0,
	y = 0,
	w = 0,
	h = 0,
}

local subtypes = { "double", "precision", "stone" }
fruitSprites = {
	double = lg.newImage("sprites/fruitJump.png"),
	precision = lg.newImage("sprites/fruitAim.png"),
	stone = lg.newImage("sprites/fruitStone.png"),
}

function Object:new(x, y, w, h, type)
	local b = {}
	b.x = x
	b.y = y
	b.w = w
	b.h = h
	b.shake = { maxTime = 0.03, timer = 0, times = 0, power = 0 }
	b.ps = ps:clone()
	b.ps:stop()
	b.type = type
	if b.type == "fruit" then
		b.subtype = subtypes[love.math.random(1, 3)]
	end

	setmetatable(b, self)
	self.__index = self
	return b
end

function Object:shift(to)
	if self.type == "block" then
		self.x = to.x + to.w + love.math.random(100, 200)
		self.y = to.y + love.math.random(-200, 0)
		self.w = love.math.random(1, 3) * 128
		self.quad = lg.newQuad(0, 0, self.w, 128, blockSprite)
		self.ps:setEmissionArea("uniform", self.w / 2, 0, 0, false)
	elseif self.type == "fruit" then
		local nextBlockId = love.math.random(20, 25)
		local nextBlock = blocks[nextBlockId]
		self.x = nextBlock.x + love.math.random(0, nextBlock.w)
		self.y = nextBlock.y + love.math.random(-300, -128)
		self.subtype = subtypes[love.math.random(1, 3)]
	end
end

function Object:update(dt, svx, svy)
	if not snek.onGround then
		if
			snek.x + svx >= self.x
			and snek.x + svx < self.x + self.w
			and snek.y + svy + snek.h / 2 >= self.y
			and snek.y + svy + snek.h / 2 < self.y + self.h
		then
			snek:collideWith(self, self.type)
			if self.type == "block" then
				self.shake.power = 5
				self.shake.times = self.shake.power * 2
				self.ps:start()
				self.ps:emit(29)
			end
		end
	end
	self.ps:update(dt)
	if self.shake.times > 0 then
		if self.shake.timer <= 0 then
			self.shake.timer = self.shake.maxTime
			self.shake.times = self.shake.times - 1
			self.shake.power = self.shake.power * -1
			self.shake.power = lerp(self.shake.power, 0, 0.2)
		else
			self.shake.timer = self.shake.timer - dt
		end
	end
end

function Object:draw()
	if self.type == "fruit" then
		lg.draw(fruitSprites[self.subtype], self.x + 16, self.y + 16, 0, 0.5, 0.5)
	end
	if self.type == "block" and self.quad then
		lg.draw(self.ps, self.x + self.w / 2, self.y + self.h / 2)
		lg.draw(blockSprite, self.quad, self.x, self.y - self.shake.power)
		if snek.precisionJumps > 0 then
			lg.color(1, 0.2, 0.2, 1)
			local sx, sy = lg.transformPoint(self.x + self.w / 3, self.y)
			lg.setScissor(sx, sy, self.w / 3, self.h * 2)
			lg.draw(blockSprite, self.quad, self.x, self.y - self.shake.power)
			lg.setScissor()
		end
	end
	lg.color(1, 1, 1)
end

function Object:drawShade()
	if self.type == "block" and self.quad then
		local offset = 7
		lg.color(0, 0, 0.3, 0.3)
		lg.draw(blockSprite, self.quad, self.x + offset, self.y - self.shake.power + offset)
	end
	lg.color(1, 1, 1)
end

function Object:drawBehind()
	if self.type == "block" and self.quad then
		local center = snek.x + lg:getWidth() / 6
		local d = self.x - center
		if self.x < snek.x + lg:getWidth() / 3 + lg:getWidth() and self.x > snek.x + lg:getWidth() / 3 then
			lg.color(0.5, 0.5, 0.5)
			lg.draw(blockSprite, self.quad, snek.x - d + lg:getWidth(), self.y)
			lg.color(1, 1, 1)
		elseif self.x + self.w < snek.x then
			lg.color(0.5, 0.5, 0.5)
			lg.draw(blockSprite, self.quad, snek.x + (snek.x - self.x) - lg:getWidth(), self.y)
			lg.color(1, 1, 1)
		end
	end
end

return Object
